<?php

namespace App\Http\Controllers\Auth;

use App\User;
use Session;
use App\Models\User as CheckUser;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\Http\Request;
class RegisterController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Register Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles the registration of new users as well as their
    | validation and creation. By default this controller uses a trait to
    | provide this functionality without requiring any additional code.
    |
    */

    use RegistersUsers;

    /**
     * Where to redirect users after registration.
     *
     * @var string
     */
    protected $redirectTo = '/mobile/index';

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest');
    }

    //重新定义注册方式
    public function register(Request $request)
    {
        //验证传入参数
        $sessionMobile = Session::get('sms.mobile');
        $sessionValidateCode = Session::get('sms.validateCode');
        if($request->mobile != $sessionMobile || $request->validateCode != $sessionValidateCode){
            return ["code"=>10001,"info"=>"验证码错误","data"=>[]];
        }
        //验证用户是否存在
        $is_user = CheckUser::where('mobile',$request->mobile)->get()->toArray();
        if($is_user){
            return ["code"=>10003,"info"=>"已存在该用户","data"=>[]];
        }
        $user = $this->create($request->all());
        if(!$user){
            return ["code"=>10002,"info"=>"注册失败","data"=>[]];
        }

        return ["code"=>10000,"info"=>"注册成功","data"=>[]];
    }

    /**
     * Get a validator for an incoming registration request.
     *
     * @param  array  $data
     * @return \Illuminate\Contracts\Validation\Validator
     */
    protected function validator(array $data)
    {
        return Validator::make($data, [
            //'name' => 'required|string|max:255',
            'mobile' => 'required|string|unique:users',
            'password' => 'required|string|min:6|confirmed',
        ]);

    }

    /**
     * Create a new user instance after a valid registration.
     *
     * @param  array  $data
     * @return \App\User
     */
    protected function create(array $data)
    {
        return User::create([
            'name' => $data['mobile'],
            'mobile' => $data['mobile'],
            'password' => Hash::make($data['password']),
        ]);
    }

    //发送验证码
    public function sendSmsCode(Request $request) {
        
        if(preg_match("/^1[345678]{1}\d{9}$/",$request->mobile)){
            //判断用户
            $is_user = CheckUser::where('mobile',$request->mobile)->get()->toArray();
            if($is_user){
                return ["code"=>10002,"info"=>"已存在该用户","data"=>[]];
            }
            //生成验证码存入session
            $code = rand(100000,999999);
            Session::put(['sms' => [
                'mobile'=>$request->mobile,
                'validateCode' => $code
            ]]);
            //发送短信
            $url="http://send.18sms.com/msg/HttpBatchSendSM?account=vd736g&pswd=dTP8X306&mobile=".$request->mobile."&msg=%E6%82%A8%E7%9A%84%E9%AA%8C%E8%AF%81%E7%A0%81%E6%98%AF%EF%BC%9A".$code."&needstatus=true&extno=";
            file_get_contents($url);
            return ["code"=>10000,"info"=>"发送成功","data"=>[]];
        }else{
            return ["code"=>10001,"info"=>"手机号错误","data"=>[]];

        }

    }

}
